package insight.miescor.opp.domain;

import insight.miescor.annotations.Code;
import insight.miescor.annotations.Constants;
import insight.miescor.annotations.UDF;
import insight.web.delegates.PrimaveraDelegate;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.axis.utils.BeanUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.primavera.ServerException;
import com.primavera.integration.client.Session;
import com.primavera.integration.client.bo.BusinessObjectException;
import com.primavera.integration.client.bo.object.Project;
import com.primavera.integration.network.NetworkException;

public class OppReport extends Opportunity {

	@UDF(name = Constants.ProjectFields.UDF_CONTRACT_VALUE, dataType = Constants.UDFDataType.COST)
	private double contractValue;

	@Code(name = Constants.ProjectFields.RISK_RATING)
	private CodeValue riskRating;

	@Code(name = Constants.ProjectFields.IS_COMPULSIVE_WIN)
	private CodeValue isCompulsiveWin;

	private Date oppInitiatedDate;

	// @Code(name = Constants.ProjectFields.pr)
	private String oppStatus;

	public OppReport(Session session, Project project)
			throws BusinessObjectException, ServerException, NetworkException {
		super(session, project);

		PrimaveraDelegate.getDataFromP6(session, project, this);
	}

	public OppReport(Session session, Project project, Opportunity opp) {
		super(opp);
		PrimaveraDelegate.getDataFromP6(session, project, this);
	}

	public double getContractValue() {
		return contractValue;
	}

	public void setContractValue(double contractValue) {
		this.contractValue = contractValue;
	}

	public CodeValue getRiskRating() {
		return riskRating;
	}

	public void setRiskRating(CodeValue riskRating) {
		this.riskRating = riskRating;
	}

	public CodeValue getIsCompulsiveWin() {
		return isCompulsiveWin;
	}

	public void setIsCompulsiveWin(CodeValue isCompulsiveWin) {
		this.isCompulsiveWin = isCompulsiveWin;
	}

	public Date getOppInitiatedDate() {
		return oppInitiatedDate;
	}

	public void setOppInitiatedDate(Date oppInitiatedDate) {
		this.oppInitiatedDate = oppInitiatedDate;
	}

	public String getOppStatus() {
		return oppStatus;
	}

	public void setOppStatus(String oppStatus) {
		this.oppStatus = oppStatus;
	}

	public JSONObject toJson() throws JSONException {
		JSONObject jObj = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		jObj.put("Title", this.getTitle())
				.put("Package Name", this.getPackageName())
				.put("Contract No.", this.getContractNo())
				.put("Location",
						(this.getLocation() != null ? this.getLocation()
								.getValue() : ""))
				.put("Contract Value", this.getContractValue())
				.put("Customer",
						(this.getCustomer() != null ? this.getCustomer()
								.getValue() : ""))
				.put("Portfolio",
						(this.getPortfolio() != null ? this.getPortfolio()
								.getValue() : ""))
				.put("Project Type",
						(this.getProjectType() != null ? this.getProjectType()
								.getValue() : ""))
				.put("Risk",
						(this.getRiskRating() != null ? this.getRiskRating()
								.getValue() : "0"))
				.put("Compulsive Win",
						(this.getIsCompulsiveWin() != null ? this
								.getIsCompulsiveWin().getValue() : ""))
				.put("InitiatedBy",
						(this.getUserId() != null ? this.getUserId() : ""))
				// .put("InitiatedOn", this.getOppInitiatedDate())
				.put("Bid Date", (this.getBidDate() != null ? sdf.format(this
						.getBidDate()) : null))
				.put("Award Date",
						(this.getAwardDate() != null ? sdf.format(this
								.getAwardDate()) : null));
		return jObj;

	}
}
